<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="商品名称" prop="partName">
                <el-input v-model="queryParams.partName" placeholder="请输入商品名称" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>

            <el-form-item>
                <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['shop:shop:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['shop:shop:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['shop:shop:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['shop:shop:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="shopList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="商品名称" align="center" prop="shopName" />
            <el-table-column label="产品分类" align="center" prop="partType">
            </el-table-column>
            <el-table-column label="所属商家" align="center" prop="cmpName" />


            <el-table-column label="原价" align="center" prop="oldPrice" />


            <el-table-column label="现价" align="center" prop="nowPrice" />



            <el-table-column label="库存" align="center" prop="partLast"  />


            <el-table-column label="销量" align="center" prop="partSaled"  />



            <el-table-column label="品牌" align="center" prop="carLogoName" />


            <el-table-column label="是否原装" align="center" prop="isOri" />


            <el-table-column label="用车类型" align="center" prop="carTypeName">
               
            </el-table-column>

            <el-table-column label="上架" align="center" prop="status">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['shop:shop:edit']">上架</el-button>
                </template>
            </el-table-column>

            <el-table-column label="录入时间" align="center" prop="updateTime" />

            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['shop:shop:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['shop:shop:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

        <!-- 添加或修改店铺管理对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="店铺名称" prop="shopName">
                    <el-input v-model="form.shopName" placeholder="请输入店铺名称" />
                </el-form-item>
                <el-form-item label="店铺logo" prop="shopLog">
                    <el-upload v-model="form.shopLog" class="avatar-uploader" :action="upload.url"
                        :headers="upload.headers" :show-file-list="false" :on-success="handleAvatarSuccess"
                        :before-upload="beforeAvatarUpload">
                        <img v-if="form.shopLog" :src="'../'+picProxy+form.shopLog" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
                <el-form-item label="联系方式" prop="tel">
                    <el-input v-model="form.tel" placeholder="请输入联系方式" />
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="form.email" placeholder="请输入邮箱" />
                </el-form-item>
                <el-form-item label="标签" prop="labelId">
                    <el-input v-model="form.labelId" placeholder="请输入标签" />
                </el-form-item>
                <el-form-item label="经营类型" prop="runTypeId">
                    <el-select v-model="form.runTypeId" placeholder="请选择经营类型">
                        <el-option v-for="dict in runTypeIdOptions" :key="dict.dictValue" :label="dict.dictLabel"
                            :value="parseInt(dict.dictValue)"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="店铺类型" prop="shopTypeId">
                    <el-select v-model="form.shopTypeId" placeholder="请选择店铺类型">
                        <el-option v-for="dict in shopTypeIdOptions" :key="dict.dictValue" :label="dict.dictLabel"
                            :value="parseInt(dict.dictValue)"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="公司名称" prop="cmpName">
                    <el-input v-model="form.cmpName" placeholder="请输入公司名称" />
                </el-form-item>

                <el-form-item label="是否关闭" prop="status">
                    <el-switch active-value="0" inactive-value="1" v-model="form.status"></el-switch>
                </el-form-item>



                <el-form-item label="店铺等级" prop="shopLevel">
                    <el-rate v-model="form.shopLevel" allow-half></el-rate>
                </el-form-item>



                <el-form-item label="主营业务" prop="shopInfo">
                    <el-input v-model="form.shopInfo" type="textarea" placeholder="请输入内容" />
                </el-form-item>
                <!--<el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>-->
                <!-- <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
                </el-form-item> -->
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import { listShop, getShop, delShop, addShop, updateShop, exportShop } from "@/api/shop/shop";
    import { getToken } from "@/utils/auth";
    export default {
        name: "Shop",
        data() {
            return {
                picProxy: process.env.VUE_APP_BASE_API.replace('/', ''),
                upload: {
                    // 是否禁用上传
                    isUploading: false,
                    // 设置上传的请求头部
                    headers: { Authorization: "Bearer " + getToken() },
                    // 上传的地址
                    url: process.env.VUE_APP_BASE_API + "/common/upload",
                    // 上传的文件列表
                    fileList: []
                },
                // 遮罩层
                loading: true,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 店铺管理表格数据
                shopList: [],
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                // 经营类型字典
                runTypeIdOptions: [],
                // 店铺类型字典
                shopTypeIdOptions: [],
                // 状态字典
                statusOptions: [],
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    shopLog: null,
                    shopName: null,
                    tel: null,
                    email: null,
                    partName: null,
                    labelId: null,
                    runTypeId: null,
                    shopTypeId: null,
                    cmpName: null,
                    shopLevel: null,
                    shopInfo: null,
                    status: null,
                },
                // 表单参数
                form: {},
                // 表单校验
                rules: {
                }
            };
        },
        created() {
            this.getList();
            this.getDicts("shop_run_type").then(response => {
                this.runTypeIdOptions = response.data;
            });
            this.getDicts("shop_type").then(response => {
                this.shopTypeIdOptions = response.data;
            });
            this.getDicts("carparts_yes_no").then(response => {
                this.statusOptions = response.data;
            });
        },
        methods: {
            /** 开关 */
            switchChange(item) {
                updateShop({ shopId: item.shopId, status: item.status }).then(response => {
                    this.msgSuccess("修改成功");
                    this.getList();
                });
            },

            /** 图片上传 */
            handleAvatarSuccess(res, file) {
                this.form.shopLog = res.fileName;
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';

                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG格式!');
                }

                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },

            /** 查询店铺管理列表 */
            getList() {
                this.loading = true;
                listShop(this.queryParams).then(response => {
                    this.shopList = response.rows;
                    this.total = response.total;
                    this.loading = false;
                });
            },
            // 经营类型字典翻译
            runTypeIdFormat(row, column) {
                return this.selectDictLabel(this.runTypeIdOptions, row.runTypeId);
            },
            // 店铺类型字典翻译
            shopTypeIdFormat(row, column) {
                return this.selectDictLabel(this.shopTypeIdOptions, row.shopTypeId);
            },
            // 状态字典翻译
            statusFormat(row, column) {
                return this.selectDictLabel(this.statusOptions, row.status);
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                    shopId: null,
                    shopLog: null,
                    tel: null,
                    email: null,
                    labelId: null,
                    runTypeId: null,
                    shopTypeId: null,
                    cmpName: null,
                    shopLevel: null,
                    shopInfo: null,
                    status: "0",
                    createBy: null,
                    createTime: null,
                    updateBy: null,
                    updateTime: null,
                    remark: null
                };
                this.resetForm("form");
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.handleQuery();
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.ids = selection.map(item => item.shopId)
                this.single = selection.length !== 1
                this.multiple = !selection.length
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.reset();
                this.open = true;
                this.title = "添加店铺管理";
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                const shopId = row.shopId || this.ids
                getShop(shopId).then(response => {
                    this.form = response.data;
                    this.open = true;
                    this.title = "修改店铺管理";
                });
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        //this.form.shopLog = this.form.shopLog.replace(process.env.VUE_APP_BASE_API,'');
                        if (this.form.shopId != null) {
                            updateShop(this.form).then(response => {
                                this.msgSuccess("修改成功");
                                this.open = false;
                                this.getList();
                            });
                        } else {
                            addShop(this.form).then(response => {
                                this.msgSuccess("新增成功");
                                this.open = false;
                                this.getList();
                            });
                        }
                    }
                });
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const shopIds = row.shopId || this.ids;
                this.$confirm('是否确认删除店铺管理编号为"' + shopIds + '"的数据项?', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function () {
                    return delShop(shopIds);
                }).then(() => {
                    this.getList();
                    this.msgSuccess("删除成功");
                })
            },
            /** 导出按钮操作 */
            handleExport() {
                const queryParams = this.queryParams;
                this.$confirm('是否确认导出所有店铺管理数据项?', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function () {
                    return exportShop(queryParams);
                }).then(response => {
                    this.download(response.msg);
                })
            }
        }
    };
</script>
<style>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }

    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>